﻿#pragma execution_character_set("utf-8")
#include "course.h"
#include <QHBoxLayout>
#include <QHeaderView>
#include "third/singleton_cpp/include/singleton.h"
#include "src/database/cdatabase.h"

Course::Course(QWidget *parent)
    : QWidget{parent}
{
    initUI();
    initData();
}

Course::~Course()
{

}

void Course::initUI()
{
    m_pTableWidget = new QTableWidget(this);
    QStringList headers;
    headers<<"星期一"<<"星期二"<<"星期三"<<"星期四"<<"星期五";
    m_pTableWidget->setColumnCount(headers.size());
    m_pTableWidget->setHorizontalHeaderLabels(headers);

    headers.clear();
    headers<<"上午 第一节"<<"上午 第二节"<<"上午 第三节"<<"上午 第四节";
    m_pTableWidget->setRowCount(headers.size());
    m_pTableWidget->setVerticalHeaderLabels(headers);
    m_pTableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    QHBoxLayout* h_layout = new QHBoxLayout();
    h_layout->addWidget(m_pTableWidget);
    setLayout(h_layout);
}

void Course::initData()
{
    std::vector<DB::course>course_data = singleton<CDataBase>().getDB()->query<DB::course>();
    int row = 0;
    for(auto iter = course_data.begin();iter != course_data.end();iter++)
    {
        QTableWidgetItem* p_item = nullptr;
        p_item = new QTableWidgetItem(iter->Monday.c_str());
        m_pTableWidget->setItem(row,0,p_item);
        p_item = new QTableWidgetItem(iter->Tuesday.c_str());
        m_pTableWidget->setItem(row,1,p_item);
        p_item = new QTableWidgetItem(iter->Wed.c_str());
        m_pTableWidget->setItem(row,2,p_item);
        p_item = new QTableWidgetItem(iter->Thursday.c_str());
        m_pTableWidget->setItem(row,3,p_item);
        p_item = new QTableWidgetItem(iter->Friday.c_str());
        m_pTableWidget->setItem(row,4,p_item);

        row++;
    }
}

